package com.example.permission.myjbm.activitys;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.permission.myjbm.adapter.MyFragmentAdapter;
import com.example.permission.myjbm.model.MyFragmentModel;
import com.example.permission.myjbm.net.ReleaseNet;
import com.example.permission.myjbm.utils.LoginSharedPreferences;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.BusinessEntity;
import com.jbm.jbmsupplier.util.IBusinessCtrl;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.IInformationCtrl;
import com.jbm.jbmsupplier.util.InfomationEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase.gSocket;


/**
 * 搜索页
 */
public class SearchAty extends ActivityUse implements View.OnClickListener{
private ImageView search_back,lv_default;
    private PullToRefreshListView lv;
    private EditText search_et;
    private ImageView search_commit;
    private List<InfomationEntity> data;
    private int page,address;
    private ICtrl info;
    private ReleaseNet net;
    private MyFragmentAdapter adapter;
    private DialogLoading load;
    private MyFragmentModel model;
    private Spinner search_select;
    //内容还是名称的判断 0代表内容  1名称
    private int chantName=0;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
        switch (msg.what){
            //商品列表
            case ReleaseNet.SEARCHERROR:
                IInformationCtrl arr= (IInformationCtrl) msg.obj;
                List<InfomationEntity>  date=arr.infomationList;
//                    praiseList; // 点赞列表
//                    focusList; //收藏列表
                //是否登录
                if(sp.getLoginState()== true){
                    //判断当前用户是否收藏
                    for(int i=0;i<date.size();i++){
                        model.modifyDate(date.get(i),df);
                        for(int j=0;j<date.get(i).getFocusList().size();j++){
                            if(gSocket.login_id==date.get(i).getFocusList().get(j).getLoginId()){
                                date.get(i).setCollect(true);
                            }
                        }
                        //判断当前用户是否点赞
                        for(int j=0;j<date.get(i).getPraiseList().size();j++){
                            if(gSocket.login_id==date.get(i).getPraiseList().get(j).getLoginId()){
                                date.get(i).setAttention(true);
                            }
                        }
                    }
                    //处理时间
                }else{
                    for(int i=0;i<date.size();i++){
                        date.get(i).setDealtime("0");
                    }
                }
                if(page==0){
                    data.clear();
                    data.addAll(date);
                    if(data.size()==0){
                        lv_default.setVisibility(View.VISIBLE);
                    }else{
                        lv_default.setVisibility(View.GONE);
                    }
                }else{
                    if(date.size()!=0){
                        data.addAll(date);
                    }
                }
                lv.onRefreshComplete();
                adapter.setNotify(data);
                break;
            //收藏回调
            case ReleaseNet.SHOUCANGERROR:
                info=(ICtrl)msg.obj;
                load.setTextstr(info.errorinfo);
                adapter.netBack(info.back,"shoucang");

                break;
            //点赞回掉
            case ReleaseNet.LICKERROR:
                info=(ICtrl)msg.obj;
                load.setTextstr(info.errorinfo);
                adapter.netBack(info.back,"dianzan");

                break;
            //商家列表
            case ReleaseNet.SEARCHMECHANTERROR:
                IBusinessCtrl arr1= (IBusinessCtrl) msg.obj;
                List<BusinessEntity> arr2=(List<BusinessEntity>)arr1.businessEntityList;



                break;

        }
            load.dismiss();
        }
    };
    private LoginSharedPreferences sp;
    private SimpleDateFormat df;


//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.aty_search);
//        initData();
//        initViews();
//
//    }
    //初始化数据
    private void initData() {
        page=0;
        address=getIntent().getIntExtra("address",7152);
        sp=new LoginSharedPreferences(SearchAty.this);
        df = new SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA);
        model=new MyFragmentModel();

    }

    @Override
    protected int setContentView1() {
        return R.layout.aty_search;
    }

    @Override
    protected void initView() {
        initData();
        initViews();
    }

    //初始化View
    private void initViews() {
        load=new DialogLoading(SearchAty.this);
            lv= (PullToRefreshListView)findViewById(R.id.search_lv);
        //返回
        search_back=(ImageView)findViewById(R.id.search_back);
        search_et=(EditText)findViewById(R.id.search_et);
        search_commit=(ImageView) findViewById(R.id.search_commit);
        lv_default=(ImageView)findViewById(R.id.lv_default);
        //Spring
        search_select=(Spinner)findViewById(R.id.search_select);
        ArrayList<String> spList = new ArrayList<>();
        spList.add("内容");
        spList.add("昵称");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchAty.this, android.R.layout.simple_spinner_item, spList);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //第四步：将适配器添加到下拉列表上
        search_select.setAdapter(adapter);
        search_select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?>parent, View view, int position, long id) {
                chantName=position;

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        initListener();
        huidiao();
    }
    //回调接口
    private void huidiao() {
        if(adapter==null){
            return ;
        }
        adapter.setRequestOption(new MyFragmentAdapter.RequestOption() {
            @Override
            public void dianzan(int infoid, int loginid, int praiseType) {
                load.show();
                net.like(infoid,loginid,praiseType);
            }

            @Override
            public void shoucang(int infoid, int loginid, int praiseType) {
                load.show();
                net.collect(infoid,loginid,praiseType);
            }
        });
    }
    //初始化监听
    private void initListener() {
        net=new ReleaseNet(handler);
        search_commit.setOnClickListener(this);
        search_back.setOnClickListener(this);
        if(data==null){
            data=new ArrayList<InfomationEntity>();
        }
        adapter = new MyFragmentAdapter(data, SearchAty.this,false);
        lv.setAdapter(adapter);
        net.search("", address,page,5);
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                switch (chantName){
                    case 0:
                        net.search(search_et.getText().toString(), address,page,5);
                        break;
                    case 1:
                        net.setSearchChant(search_et.getText().toString(),page);
                        break;
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page+=5;
                switch (chantName){
                    case 0:
                        net.search(message, address,page,5);
                        break;
                    case 1:
                        net.setSearchChant(search_et.getText().toString(),page);
                        break;
                }


            }
        });
        lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                page+=5;
                switch (chantName){
                    case 0:
                        net.search(message, address,page,5);
                        break;
                    case 1:
                        net.setSearchChant(search_et.getText().toString(),page);
                        break;
                }


            }
        });
    }

    private String message="";

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.search_back:
                SearchAty.this.finish();
                    break;
            case R.id.search_commit:
                load.show();
                page=0;
                switch (chantName){
                    case 0:
                    net.search(search_et.getText().toString(), address,0,5);
                    message=search_et.getText().toString();
                        break;
                    case 1:
                        net.setSearchChant(search_et.getText().toString(),page);
                        message=search_et.getText().toString();
                        break;
                }
                break;

        }


    }
}
